package com.example.schoolmanage.controller;


import com.example.schoolmanage.domain.PageResult;
import com.example.schoolmanage.domain.ResultData;
import com.example.schoolmanage.domain.Student;
import com.example.schoolmanage.domain.StudentAdd;
import com.example.schoolmanage.mapper.StudentMapper;
import com.example.schoolmanage.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 阿远123
 * @since 2023-01-28
 */
@SuppressWarnings("deprecation")
@Api(value = "学生管理", description = "提供学生管理相关接口", tags = "学生管理接口")
@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    StudentMapper studentMapper;

    @ApiOperation(value = "通过学号查询学生信息", notes = "获取学生信息", response = ResultData.class)
    @RequestMapping(value = "/student1",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")//对该接口赋予用户角色限制，只有admin角色的用户才能访问
    public ResultData student1(@RequestParam("id")Integer id) {
        //Integer id =1;
        try {
            Student student =studentService.selectOneByStuId(id);
            return new ResultData(true,"查询成功", student);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(true,"查询失败",null);
        }
    }
    @ApiOperation(value = "通过学号查询学生和对应的课程信息", notes = "获取学生及对应的课程信息", response = ResultData.class)
    @RequestMapping(value = "/getStuCous",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData getStuCous(@RequestParam("id") Integer id){//@param是mybatis的注解
        try {
            Student student =studentService.getStuCousbyStuId(id);
            return new ResultData(true,"查询成功", student);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(true,"查询失败",null);
        }
    }
    @ApiOperation(value = "通过学号查询学生信息及对应的课程编号", notes = "获取学生及对应的课程编号", response = ResultData.class)
    @RequestMapping(value = "/getstucous2",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData getstucous2(@RequestParam Integer sId){//@RequestParam是springmvc的注解
        try {
            Student student =studentService.getStubyStuId(sId);
            return new ResultData(true,"查询成功",  student);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(true,"查询失败");
        }
    }

    @ApiOperation(value = "添加学生信息", notes = "只需要传姓名和性别，id不需要传默认0就行", response = ResultData.class)
    @PostMapping("/addstudent")
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData addstudent(@Valid @RequestBody Student Stu){//@RequestBody是springmvc的注解


        boolean b = studentService.addStudent(Stu);
        //ResultData resultData = new ResultData();
        //return resultData(200)
        if (b){
            return new ResultData(true,"添加成功",null);
        }
        return new ResultData(false,"添加失败");
    }

    @ApiOperation(value = "添加学生信息，优化了传参", notes = "优化了传参", response = ResultData.class)
    @PostMapping("/addstudent2")
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData addstudent2(@Valid @RequestBody StudentAdd studentAdd){//@RequestBody将json转化为实体类对象

        boolean b = studentService.addStudent2(studentAdd);
        //ResultData resultData = new ResultData();
        //return resultData(200)
        if (b){
            return new ResultData(true,"添加成功");
        }
        return new ResultData(false,"添加失败");
    }
    @ApiOperation(value = "分页查询学生信息", notes = "分页查询学生信息", response = ResultData.class)
    @RequestMapping(value = "/getallpage",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData getallpage(@ApiParam(value = "当前页码")@RequestParam(defaultValue = "1") Integer pagenum,
                                    @ApiParam(value = "当前页码")@RequestParam(defaultValue = "3") Integer pagesize){

        List<Student> students = studentService.getallbypage(pagenum, pagesize);

        return new ResultData(true,"添加成功", students);


    }

    @ApiOperation(value = "分页查询学生信息2,返回页码和集合", notes = "分页查询学生信息2", response = ResultData.class)
    @RequestMapping(value = "/getallVpage",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")
    public PageResult getallVpage(@ApiParam(value = "当前页码")@RequestParam(defaultValue = "1") Integer pagenum,
                                 @ApiParam(value = "当前页码")@RequestParam(defaultValue = "3") Integer pagesize){

        PageResult <Student> students = studentService.getallVpage(pagenum, pagesize);

        return students;


    }
    @ApiOperation(value = "查询全部学生信息", notes = "查询全部学生信息", response = ResultData.class)
    @RequestMapping(value = "/getallStudent",method = RequestMethod.GET)
    @PreAuthorize("hasRole('ADMIN')")
    public ResultData getallStudent(){
        List<Student>students = studentMapper.getallStudent();
        return new ResultData(true,"查询成功", students);
    }
    @ApiOperation(value = "分页学生和对应的课程信息", notes = "分页学生和对应的课程信息", response = ResultData.class)
    @RequestMapping(value = "/getStuCouspage",method = RequestMethod.GET)
    //@PreAuthorize("hasRole('ADMIN')")
    public ResultData getStuCouspage(@ApiParam(value = "当前页码") @RequestParam(defaultValue = "1") Integer pagenum,
                                       @ApiParam(value = "每页数据条数")@RequestParam(defaultValue = "3") Integer pagesize){
        List<Student>students = studentService.getStuCouspage(pagenum,pagesize);
        return new ResultData(true,"查询成功", students);
    }





}

